package com.tledu.spring.core.likou;

import java.util.BitSet;

/**
 * @author Lsj
 * @version 1.0
 * @date 2021/7/8 20:57
 */
public class Sieve {
    public static void main(String[] args) {
        // 给一个范围
        int n = 20000000;
        long start = System.currentTimeMillis();
        BitSet bitSet = new BitSet(n+1);
        int count = 0;
        int i;
        for (i=2;i<=n;i++){
            bitSet.set(i);
        }
        i = 2;
        while (i*i<=n){
            if (bitSet.get(i)){
                count++;
                int k = 2*i;
                while (k<=n){
                    bitSet.clear(k);;
                    k+=i;
                }
            }
            i++;
        }
        while (i<=n){
            if (bitSet.get(i)){
                count++;
            }
            i++;
        }
        long end = System.currentTimeMillis();
        System.out.println(count+"primes");
        System.out.println((end-start)+"milliseconds");
    }
}
